home *** CD-ROM | disk | FTP | other *** search
- Path: news.ov.com!news
- From: glenn@ov.com (Fletcher.Glenn@ov.com)
- Newsgroups: comp.lang.c
- Subject: Re: sizeof() question >>> :)
- Date: 12 Apr 1996 15:50:42 GMT
- Organization: OpenVision
- Message-ID: <4klu4i$7mc@spanky.pls.ov.com>
- References: <1996Apr12.061927@topaz>
- Reply-To: glenn@ov.com
- NNTP-Posting-Host: foghorn.pls.ov.com
-
- In article 061927@topaz, naderr@topaz.cqu.edu.au writes:
- >Hi,
- >
- >How can I get, with a pointer, the sizeof of an array that is
- >pointed by the pointer ? (No it's not a tounge twister :)
- >
- >Ok, let see if with an example I could clarify what I'm trying to say,
- >
- >
- > char First_name[20];
- > char Last_name[20];
- > char Address[60];
- > char Phone[20];
- > char **ct, *cp;
- > char *record[4];
- > char ch;
- >
- > record[0] = First_name;
- > record[1] = Last_name;
- > record[2] = Address;
- > record[3] = Phone;
- > ct = record;
- > cp = *ct;
- >
- > while (!done) {
- > ch = getchar();
- > if (cp - *ct < sizeof(XXXXXXX)-1) {
- > *cp++ = ch;
- > } else {
- > beep();
- > }
- > }
- >
- >for XXXXXXX the _only_ thing that I can find that works is to stick
- >in there is the actual array that cp is pointing to, i.e. sizeof(First_name).
- >
- >What ideas do you have so that I can get the size of the array currently
- >pointed to by cp?
- >
- >Any ideas greatly appreciated,
- >
- >please email direct to me (as well if you wish to followup) at
- >
- > naderr@topaz.cqu.edu.au
- >
- >TIA,
- >
- > Rob
- >
- >
-
-
-
- Sizeof is a compile time directive, and as such it cannot be used in a
- dynamic fashion at run time. I believe that the proper solution to your
- problem is to create a struct definition like the one below:
-
- struct record
- {
- char *string;
- int storage_allowed;
- };
-
- Then you can use malloc() to provide the variable length storage
- for each of your arrays, and use the value of "storage_allowed" to
- act as the limit value in your input loop.
-
- Fletcher.Glenn@ov.com
-
-